// cloud/functions.js - 云函数封装
class CloudFunctions {
  constructor() {
    this.cloud = wx.cloud
  }

  // 调用云函数
  async callFunction(name, data = {}) {
    try {
      const result = await this.cloud.callFunction({
        name: name,
        data: data
      })
      return result.result
    } catch (error) {
      console.error('调用云函数失败:', error)
      throw error
    }
  }

  // 用户登录
  async userLogin(userInfo) {
    return this.callFunction('userLogin', {
      userInfo: userInfo
    })
  }

  // 获取用户信息
  async getUserInfo(openid) {
    return this.callFunction('getUserInfo', {
      openid: openid
    })
  }

  // 同步任务数据
  async syncTasks(tasks, userId) {
    return this.callFunction('syncTasks', {
      tasks: tasks,
      userId: userId
    })
  }

  // 生成日报
  async generateReport(userId, date, type = 'daily') {
    return this.callFunction('generateReport', {
      userId: userId,
      date: date,
      type: type
    })
  }

  // 导出数据
  async exportData(userId, format = 'csv') {
    return this.callFunction('exportData', {
      userId: userId,
      format: format
    })
  }
}

module.exports = CloudFunctions